﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseUI;
using Utility;
using Utility.Model;
using Hxj.Data;

namespace MsgCenter.Web.Pages.UserGroup
{
    public partial class Option : BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strGuID = ParamsOfRequest.Request("Guid");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (strType == "Add")
                {
                    this.linkSave.Visible = true;
                }
                else if (strType == "Edit")
                {
                    this.linkSave.Visible = true;
                    this.GetModel();
                }
                else if (strType == "Look")
                {
                    this.GetModel();
                }
            }
        }

        private void GetModel()
        {
            Sys_Group model = BLL.BLLServiceHelper.GetGroupService().GetModel(strGuID);
            if (model != null)
            {
                txtName.Value = model.GroupName;
                txtDes.Text = model.GroupDes;
            }
        }

        protected void linkSave_Click(object sender, EventArgs e)
        {
            try
            {
                Sys_Group model = null;
                if (strType == "Add")
                {
                    model = BLL.BLLServiceHelper.GetBaseDataService().GetModel<Sys_Group>(new WhereClip().And(Sys_Group._.IsDelete == false && Sys_Group._.GroupName == txtName.Value));
                    if (model != null)
                    {
                        base.ShowAlert("保存数据出错:已经存在相同分组名称");
                        return;
                    }
                }
                if (strGuID != "")
                {
                    model = BLL.BLLServiceHelper.GetGroupService().GetModel(strGuID);
                }
                else
                {
                    model = new Sys_Group();
                }
                model.GroupDes = txtDes.Text;
                model.GroupName = txtName.Value;
                model.CreateDate = DateTime.Now;
                model.CreateUser = base.Account;
                BLL.BLLServiceHelper.GetGroupService().Save(model, null);
                base.ShowAlertScript("Close_Reload();");
            }
            catch (Exception ex)
            {
                base.ShowAlert("保存数据出错:" + ex.Message);
            }
        }
    }
}